import { message } from "antd";
import axios from "axios";

const instance = axios.create({
  baseURL: "",
  timeout: 3000,
});

instance.interceptors.request.use(
  (config: any) => {
    //当有token时自动携带，至于有没有权限限制就看返回的状态码
    const token = localStorage.getItem("jiang_blog_token");
    config.headers.Authorization = `Bearer ${token}`;
    return config;
  },
  (err) => {}
);

instance.interceptors.response.use(
  (res) => {
    return res.data;
  },
  (err) => {
    if (err && err.response) {
      switch (err.response.status) {
        case 400:
          console.log("请求错误");
          break;
        case 401:
          message.error("请登录");
          break;
        default:
          console.log("其他错误信息");
      }
    }
    return Promise.reject(err);
  }
);

export const request = instance;
